#pragma once
#include "vtkslicerlogic.h"
#include "windows.h"
#include "vtkSlicerApplication.h"
#include <string>
#include <vector>
#include <map>
#include "igstkPivotCalibration.h"
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
class EndoscopeGUI;
class CNDIControl;
class vtkMatrix4x4;
class vtkMRMLLinearTransformNode;
class EndoscopeLogic :
	public vtkSlicerLogic
{
public:
	// The Usual vtk class functions
	static EndoscopeLogic *New();
	 vtkTypeRevisionMacro(EndoscopeLogic,vtkSlicerLogic);
	vtkSetObjectMacro(Application, vtkSlicerApplication);
	vtkGetObjectMacro(Application, vtkSlicerApplication);
	vtkSetMacro(ModuleGUI, EndoscopeGUI *);
	vtkGetMacro(Buffer, char *);
	vtkGetMacro(ImageCardHandle, HANDLE);
	vtkGetObjectMacro(Matrix1, vtkMatrix4x4);
	vtkBooleanMacro(IsCalibrating, bool);
	vtkSetMacro(IsCalibrating, int);
	vtkGetMacro(CvImage, IplImage *);

	// Description
	// All logic classes need to know about the current mrml scene
	

	virtual void ProcessMRMLEvents ( vtkObject * /*caller*/, 
		unsigned long /*event*/, void * /*callData*/ );

	virtual void ProcessLogicEvents( vtkObject * /*caller*/, 
		unsigned long /*event*/, void * /*callData*/ );

	virtual void ProcessLogicEvents() {};

	typedef igstk::PivotCalibration PivotCalibrationType;
	typedef PivotCalibrationType::VersorType VersorType;
	typedef PivotCalibrationType::VectorType VectorType;
	typedef PivotCalibrationType::PointType PointType;
	typedef PivotCalibrationType::ErrorType ErrorType;
	/*typedef itk::Logger LoggerType;
	typedef itk::StdStreamLogOutput LogOutputType;*/

	// Additional functionality:

public:
	virtual ~EndoscopeLogic();

protected:
	HANDLE ImageCardHandle;
	char * Buffer;
	IplImage * CvImage;
	char * CvImageData;
	vtkSlicerApplication * Application;
	CNDIControl * NDIControl;
	EndoscopeGUI * ModuleGUI;
	std::string graspTimerID;
	int m_bInterference ;
	int m_bPortOccupied;
	int m_bPortUnoccupied;
	vtkMatrix4x4 * Matrix1;
	typedef std::map<int, vtkMRMLLinearTransformNode *> AuroraTransformsType;
	AuroraTransformsType AuroraTransformNodeVec;
	int IsCalibrating;
	PivotCalibrationType::Pointer Pivot; 
	EndoscopeLogic();
	EndoscopeLogic(const EndoscopeLogic&);
	void operator=(const EndoscopeLogic&);
public:
	void StartCapture(void);
	void StopCapture(void);
	void OpenDevice(const char * deviceName);
	void CloseDevice(void);
	int InitializeAurorer(void);
	void StartTracking();
	void StopTracking(void);
	void Grasp(void);
	int GetCalibrationResult(double offset[4]);
};
